<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Rent extends CI_Controller {
    function Rent() 
    {
        parent::__construct();
        $this->load->helper('html');
        $this->load->helper('url');
        $this->load->helper('form');
    }
	
    public function index()
    {
        $data['message'] = "";
        $data['make'] = $this->db->get('psacar_make');
        $data['model'] = $this->db->get('psacar_model');
        $data['category'] = $this->db->get('psacar_category');

        $this->db->order_by("order", "asc"); 
        $this->db->order_by("province", "asc"); 
        $data['location'] = $this->db->get('psacar_location');
        
        $data['trip_type'] = $this->db->get('psacar_trip_type');
        $data['bus_company'] = $this->db->get_where('psacar_page', array('keyword'=> 'Bus'));
        
        $this->load->view('header',$data);
        $this->load->view('rent',$data);
        $this->load->view('footer',$data);
    }
    
    public function result(){	
        $make = $this->input->post('make');
        $model = $this->input->post('model');
        $series = $this->input->post('series');
        $location = $this->input->post('location');
        $rent_type = $this->input->post('rent_type');
        $duration = $this->input->post('duration');
        $duration_type = $this->input->post('duration_type');
        $driver = $this->input->post('driver');
        
        //$type = $this->input->post('');
        $data['make']= $this->db->get('psacar_make');
        $this->load->model('Car_model');
        $data['message'] = "";
        $data['car_list'] = $this->Car_model->get_car_list(
            $make, 
            $model, 
            '',
            '',
            $series,
            $location,
            '%',
            $rent_type,
            $duration,
            $duration_type,
            $driver);

        $this->load->view('header',$data);
        $this->load->view('car_list_view', $data);
        $this->load->view('footer',$data);
    }
        
    public function detail()
    {
        $data['message'] = "";

        //Variable
        $record = $this->uri->segment(3);        
        
        //Query: Car list
        $this->load->model('Car_model');
        $data['car_list'] = $this->Car_model->get_car_by_id($record);

        //Query: Comment
        $this->load->model('Comment_model');
        $data['comment_list'] = $this->Comment_model->get_comments_by_car($record);	

        $this->load->view('header', $data);
        $this->load->view('templates/car/car_detail', $data);
        
        $data['make']= $this->db->get('psacar_make');
        $this->load->view('footer', $data);
    }
    
    public function process()
    {
        $email = $this->security->xss_clean($this->input->post('email'));
        $password = $this->security->xss_clean($this->input->post('password'));
         
        $this->load->model('Member_model');
        $result = $this->Member_model->login($email, $password);        

        if($result)
            $data['message'] = "";
        else
            $data['message'] = "<font color=red>Invalid username and/or password.</font>";
                                    
        $car_id = $this->uri->segment(3);
        if($car_id)
        {
            $this->load->model('Car_model');
            $data['car_list'] = $this->Car_model->get_car_by_id($car_id);

            $this->load->model('Comment_model');
            $data['comment_list'] = $this->Comment_model->get_comments_by_car($car_id);

            $this->load->view('header', $data);
            $this->load->view('car_detail_view', $data);
            $data['make']= $this->db->get('psacar_make');
            $this->load->view('footer',$data);        
        }
        else 
            redirect($this->base_url);
    }
    
    public function pre_submit()
    {
        $data['message'] = "";
        $this->load->view('templates/rent/rent_submit', $data);
    }
    
    public function submit()
    {
        $data['message'] = "";
        $request_car = $this->input->post('submit_rent_car');        
        $request_transport = $this->input->post('submit_rent_bus');        

        if($request_car || $request_transport)
        {  
            $mail_subject = '';
            $mail_message = '';

            if($request_car)
            {                
                $car_id = $this->uri->segment(3);
                $mail_subject = 'Welcome to psacar.com - Request for quotation';
                $mail_message = $this->message_car(
                    $this->input->post('email'),
                    $this->input->post('phone'),                        
                    $this->input->post('rent_type'),
                    $this->input->post('trip_type'),
                    $this->input->post('make'),
                    $this->input->post('model'),
                    $this->input->post('year'),
                    $this->input->post('from_date'),
                    $this->input->post('from_time'),
                    $this->input->post('to_date'),
                    $this->input->post('to_time'),
                    $this->input->post('location_from'),
                    $this->input->post('province_from'),
                    $this->input->post('location_to'),
                    $this->input->post('province_to'),
                    $this->input->post('number_day'),
                    $this->input->post('requirement'));
            }

            if($request_transport)
            {
                $mail_subject = 'PsaCar Marketplace - Bus Transport Inquiry';
                $mail_message = $this->message_bus(
                    $this->input->post('email'),
                    $this->input->post('phone'),                        
                    $this->input->post('trip_type'),
                    $this->input->post('location_from'),
                    $this->input->post('location_to'),
                    $this->input->post('departure_date'),
                    $this->input->post('departure_time'),
                    $this->input->post('return_date'),
                    $this->input->post('return_time'),
                    $this->input->post('company'),
                    $this->input->post('seat'));
            }

            //Send mail
            $this->sendmail(
                $this->input->post('email'), 
                $mail_subject, 
                $mail_message);

            $data['message_title'] = "Quotation inquiry";
            $data['message_body'] = "Thanks for your inquiry. Our team will get back to you shortly.";
            $this->load->view('header', $data);
            $this->load->view('information', $data);
            $data['make']= $this->db->get('psacar_make');
            $this->load->view('footer', $data);
        }
        else 
            //Unknown request
            redirect($this->base_url);
    }
    
    public function message_car(
        $email,
        $phone,                        
        $rent_type,
        $trip_type,
        $make,
        $model,
        $year,
        $from_date,
        $from_time,
        $to_date,
        $to_time,
        $location_from,
        $province_from,
        $location_to,
        $province_to,
        $number_day,
        $requirement)
    {
        if($make) {
            $this->db->limit(1);
            $query = $this->db->get_where("psacar_make", array("id"=> $make));
            $make = $query->row()->name;            
        }
        
        if($model) {
            $this->db->limit(1);
            $query = $this->db->get_where("psacar_model", array("id"=> $model));
            $model = $query->row()->name;            
        }
        
        $mail_message = 
            '<div style="padding:0px;margin:0px;font-family:' . 
            "'Helvetica Neue'" . 
            ',Helvetica,Arial;font-size:12px;color:#5e5959">';
        $mail_message .= '<table cellspacing="0" cellpadding="0" border="0" bgcolor="#f6f6f6" align="center">';
        $mail_message .= '<tbody>';
        $mail_message .= '<tr>';
        $mail_message .= '<td width="750">';
        $mail_message .= '<table width="100%" align="center">';
        $mail_message .= '<tbody>';
        $mail_message .= '<tr>';
        $mail_message .= '<td width="100%" bgcolor="#003466" style="color:#fff;padding:3px;text-align:center;font-size:10px">';
        $mail_message .= 'Contact us by phone at 096 8696 168' .              
            '&nbsp;or by email at <a target="_blank" href="mailto:contact@psacar.com" style="color:#fff" >contact@psacar.com</a>';
        $mail_message .= '</td>';
        $mail_message .= '</tr>';
        $mail_message .= '<tr>';
        $mail_message .= '<td width="100%" style="padding:20px 10px 10px 10px;text-align:center">';
        $mail_message .= '<a target="_blank" href="http://www.psacar.com">' . 
            '<img border="0" alt="psacar.com" src="http://psacar.com/system/public/img/logo.png" />' . 
            '</a>';
        $mail_message .= '</td>';
        $mail_message .= '</tr>';
        $mail_message .= '<tr>';
        $mail_message .= '<td style="padding:10px 10px 10px 10px">';
        $mail_message .= '<table width="100%">';
        $mail_message .= '<tbody>';
        $mail_message .= '<tr>';
        $mail_message .= '<td style="border:1px solid #efebec;padding:15px 15px 15px 15px">';
        $mail_message .= '<h1 style="font-size:18px;color:#4c4848;font-weight:bold">';
        $mail_message .= '<span style="margin:20px 0 0 0;line-height:150%">Your inquiry is received!</span>';
        $mail_message .= '</h1>';
        $mail_message .= '<p style="border-bottom:1px dotted #efebec;width:100%"></p>';
        $mail_message .= '<p>Greetings from <a target="_blank" href="http://www.psacar.com">www.psacar.com</a>,</p>';
        $mail_message .= '<p>Thanks for your inquiry for car rental services at ' . 
            '<a target="_blank" href="http://www.psacar.com">www.psacar.com</a>.' .
            ' Our customer service agent will contacting you shortly.';
        $mail_message .= '<p>Contact Summary:</p>';
        $mail_message .= '<p>Email: ' . $email . '</p>';
        $mail_message .= '<p>Phone: ' . $phone . '</p>';
        $mail_message .= '<p>&nbsp;</p>';
        $mail_message .= '<p>Your have requested a quotation for the following:</p>';
        $mail_message .= '<p>Retal type: ' . $rent_type . '</p>';
        $mail_message .= '<p>Make: ' . $make . '</p>';
        $mail_message .= '<p>Model: ' . $model . '</p>';
        $mail_message .= '<p>Year: ' . $year . '</p>';
        if($rent_type == 'Short Term')
        {
            $mail_message .= '<p>Request type: ' . $trip_type . '</p>';
            $mail_message .= '<p>Pickup Date & Time: ' . $from_date . ' ' . $from_time . '</p>';
            $mail_message .= '<p>Return Date & Time: ' . $to_date . ' ' . $to_time . '</p>';
            $mail_message .= '<p>Pickup address: ' . $location_from . ', ' . $province_to . '</p>';
            $mail_message .= '<p>Drop off address: ' . $location_to . ', ' . $province_to . '</p>';
        }
        else {
            $mail_message .= '<p>Number of day: ' . $number_day . '</p>';
            $mail_message .= '<p>Other requirement: ' . $requirement . '</p>';
        }
        
        $mail_message .= '<p>&nbsp;</p>';
        $mail_message .= '<p style="text-align: justify;">';
        $mail_message .= 'We always strive to improve your experience at ' .
            '<a target="_blank" href="http://www.psacar.com">www.psacar.com</a>' . 
            '.  If you have any suggestions or feedback, please contact us by e-mail ' . 
            '<a target="_blank" href="mailto:contact@psacar.com">contact@psacar.com</a> or by phone ' . 
            '<a target="_blank" href="tel:096%208696%20168">096 8696 168</a>';
        $mail_message .= '</p>';
        $mail_message .= '<p>&nbsp;</p>';
        $mail_message .= '<p>';
        $mail_message .= 'Kind Regards,<br />' . 
            '<a target="_blank" href="http://www.psacar.com">www.psacar.com</a>.  Administration Team';
        $mail_message .= '</p>';
        $mail_message .= '<p>';
        $mail_message .= 'NOTE: This email was automatically generated from '. 
            '<a target="_blank" href="http://www.psacar.com">www.psacar.com</a>.';
        $mail_message .= '</p>';
        $mail_message .= '</td>';
        $mail_message .= '</tr>';
        $mail_message .= '</tbody>';
        $mail_message .= '</table>';
        $mail_message .= '</td>';
        $mail_message .= '</tr>';
        $mail_message .= '<tr>';
        $mail_message .= '<td width="100%" style="padding:10px 0 20px 0">';
        $mail_message .= '<table align="center" style="font-size:10px">';
        $mail_message .= '<tbody>';
        $mail_message .= '<tr>';
        $mail_message .= '<td align="center">';
        $mail_message .= '<a target="_blank" href="http://www.facebook.com/pages/PSA-CAR/546725852022926">';
        $mail_message .= '<img border="0" alt="follow us on facebook" src="' . base_url() . 'system/public/img/facebook.jpg" />';
        $mail_message .= '</a>&nbsp;';
        $mail_message .= '<a target="_blank" href="http://www.twitter.com/PSACAR">';
        $mail_message .= '<img border="0" alt="follow us on twitter" src="' . base_url() . 'system/public/img/twitter.jpg" />';
        $mail_message .= '</a>';
        $mail_message .= '</td>';
        $mail_message .= '</tr>';
        $mail_message .= '<tr>';
        $mail_message .= '<td align="center">';
        $mail_message .= '&copy; 2012 <a target="_blank" href="http://www.psacar.com" >www.psacar.com</a>';
        $mail_message .= '</td>';
        $mail_message .= '</tr>';
        $mail_message .= '<tr>';
        $mail_message .= '<td align="center">';
        $mail_message .= 
            'No. 126, Norodom Boulevard, <br>' .
            '100m south of Independence Monument.';
        $mail_message .= '</td>';
        $mail_message .= '</tr>';
        $mail_message .= '</tbody>';
        $mail_message .= '</table>';
        $mail_message .= '</td>';
        $mail_message .= '</tr>';
        $mail_message .= '</tbody>';
        $mail_message .= '</table>';
        $mail_message .= '</td>';
        $mail_message .= '</tr>';
        $mail_message .= '</tbody>';
        $mail_message .= '</table>';
        $mail_message .= '</div>';        
        
        return $mail_message;            
    }
    
    public function message_bus(
        $email,
        $phone,                        
        $trip_type,
        $location_from,
        $location_to,
        $departure_date,
        $departure_time,
        $return_date,
        $return_time,
        $company,
        $seat)
    {
        $mail_message = 
            '<div style="padding:0px;margin:0px;font-family:' . 
            "'Helvetica Neue'" . 
            ',Helvetica,Arial;font-size:12px;color:#5e5959">';
        $mail_message .= '<table cellspacing="0" cellpadding="0" border="0" bgcolor="#f6f6f6" align="center">';
        $mail_message .= '<tbody>';
        $mail_message .= '<tr>';
        $mail_message .= '<td width="750">';
        $mail_message .= '<table width="100%" align="center">';
        $mail_message .= '<tbody>';
        $mail_message .= '<tr>';
        $mail_message .= '<td width="100%" bgcolor="#003466" style="color:#fff;padding:3px;text-align:center;font-size:10px">';
        $mail_message .= 'Contact us by phone at 096 8696 168' .              
            '&nbsp;or by email at <a target="_blank" href="mailto:contact@psacar.com" style="color:#fff" >contact@psacar.com</a>';
        $mail_message .= '</td>';
        $mail_message .= '</tr>';
        $mail_message .= '<tr>';
        $mail_message .= '<td width="100%" style="padding:20px 10px 10px 10px;text-align:center">';
        $mail_message .= '<a target="_blank" href="http://www.psacar.com">' . 
            '<img border="0" alt="psacar.com" src="http://psacar.com/system/public/img/logo.png" />' . 
            '</a>';
        $mail_message .= '</td>';
        $mail_message .= '</tr>';
        $mail_message .= '<tr>';
        $mail_message .= '<td style="padding:10px 10px 10px 10px">';
        $mail_message .= '<table width="100%">';
        $mail_message .= '<tbody>';
        $mail_message .= '<tr>';
        $mail_message .= '<td style="border:1px solid #efebec;padding:15px 15px 15px 15px">';
        $mail_message .= '<h1 style="font-size:18px;color:#4c4848;font-weight:bold">';
        $mail_message .= '<span style="margin:20px 0 0 0;line-height:150%">Your inquiry is received!</span>';
        $mail_message .= '</h1>';
        $mail_message .= '<p style="border-bottom:1px dotted #efebec;width:100%"></p>';
        $mail_message .= '<p>Greetings from <a target="_blank" href="http://www.psacar.com">www.psacar.com</a>,</p>';
        $mail_message .= '<p>Thanks for your inquiry for bus ticket services at ' . 
            '<a target="_blank" href="http://www.psacar.com">www.psacar.com</a>.' .
            ' Our customer service agent will contacting you shortly.';
        $mail_message .= '<p>Contact Summary:</p>';
        $mail_message .= '<p>Email: ' . $email . '</p>';
        $mail_message .= '<p>Phone: ' . $phone . '</p>';
        $mail_message .= '<p>&nbsp;</p>';
        $mail_message .= '<p>Your have requested a quotation for the following:</p>';
        $mail_message .= '<p>Leave from: ' . $location_from . '</p>';
        $mail_message .= '<p>Going to: ' . $location_to . '</p>';
        $mail_message .= '<p>Departure on: ' . $departure_date . ' ' . $departure_time . '</p>';
        if($trip_type == 'Round trip')
            $mail_message .= '<p>Returning on: ' . $return_date . ' ' . $return_time . '</p>';
        $mail_message .= '<p>Company: ' . $company . '</p>';
        $mail_message .= '<p>Number of seat: ' . $seat . '</p>';
        $mail_message .= '<p>&nbsp;</p>';
        $mail_message .= '<p style="text-align: justify;">';
        $mail_message .= 'We always strive to improve your experience at ' .
            '<a target="_blank" href="http://www.psacar.com">www.psacar.com</a>' . 
            '.  If you have any suggestions or feedback, please contact us by e-mail ' . 
            '<a target="_blank" href="mailto:contact@psacar.com">contact@psacar.com</a> or by phone ' . 
            '<a target="_blank" href="tel:096%208696%20168">096 8696 168</a>';
        $mail_message .= '</p>';
        $mail_message .= '<p>&nbsp;</p>';
        $mail_message .= '<p>';
        $mail_message .= 'Kind Regards,<br />' . 
            '<a target="_blank" href="http://www.psacar.com">www.psacar.com</a>.  Administration Team';
        $mail_message .= '</p>';
        $mail_message .= '<p>';
        $mail_message .= 'NOTE: This email was automatically generated from '. 
            '<a target="_blank" href="http://www.psacar.com">www.psacar.com</a>.';
        $mail_message .= '</p>';
        $mail_message .= '</td>';
        $mail_message .= '</tr>';
        $mail_message .= '</tbody>';
        $mail_message .= '</table>';
        $mail_message .= '</td>';
        $mail_message .= '</tr>';
        $mail_message .= '<tr>';
        $mail_message .= '<td width="100%" style="padding:10px 0 20px 0">';
        $mail_message .= '<table align="center" style="font-size:10px">';
        $mail_message .= '<tbody>';
        $mail_message .= '<tr>';
        $mail_message .= '<td align="center">';
        $mail_message .= '<a target="_blank" href="http://www.facebook.com/pages/PSA-CAR/546725852022926">';
        $mail_message .= '<img border="0" alt="follow us on facebook" src="' . base_url() . 'system/public/img/facebook.jpg" />';
        $mail_message .= '</a>&nbsp;';
        $mail_message .= '<a target="_blank" href="http://www.twitter.com/PSACAR">';
        $mail_message .= '<img border="0" alt="follow us on twitter" src="' . base_url() . 'system/public/img/twitter.jpg" />';
        $mail_message .= '</a>';
        $mail_message .= '</td>';
        $mail_message .= '</tr>';
        $mail_message .= '<tr>';
        $mail_message .= '<td align="center">';
        $mail_message .= '&copy; 2012 <a target="_blank" href="http://www.psacar.com" >www.psacar.com</a>';
        $mail_message .= '</td>';
        $mail_message .= '</tr>';
        $mail_message .= '<tr>';
        $mail_message .= '<td align="center">';
        $mail_message .= 
            'No. 126, Norodom Boulevard, <br>' .
            '100m south of Independence Monument.';
        $mail_message .= '</td>';
        $mail_message .= '</tr>';
        $mail_message .= '</tbody>';
        $mail_message .= '</table>';
        $mail_message .= '</td>';
        $mail_message .= '</tr>';
        $mail_message .= '</tbody>';
        $mail_message .= '</table>';
        $mail_message .= '</td>';
        $mail_message .= '</tr>';
        $mail_message .= '</tbody>';
        $mail_message .= '</table>';
        $mail_message .= '</div>';     
        
        return $mail_message;         
    }
    
    public function sendmail($mail_to, $subject, $mail_message)
    {        
        $this->load->library('email');
        
        $this->email->from('booking@psacar.com', 'www.psacar.com');
        $this->email->to('booking@psacar.com'); 
        if($mail_to != '')
            $this->email->bcc($mail_to); 

        $this->email->subject($subject);
        $this->email->message($mail_message);	

        $this->email->set_mailtype("html");
        $this->email->send();
        
        //echo $this->email->print_debugger();
        //echo $mail_message;
    }
}